//
//  WYPutForwardController.m
//  Boss
//
//  Created by admin on 2018/7/7.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYPutForwardController.h"
#import "WYPutForward.h"
#import "WYPutForwardCell.h"
#import "WYPayAlertTooler.h"
@interface WYPutForwardController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *mainView;

@property (strong, nonatomic) IBOutlet UILabel *balanceLabel;

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *top;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;

@property (nonatomic,strong)NSString *drawID;

@property (strong, nonatomic) IBOutlet UIButton *putBtn;
@property (strong, nonatomic) IBOutlet UITextField *moenyTextField;
@property (strong, nonatomic) IBOutlet UILabel *messageLabel;

@end

@implementation WYPutForwardController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getDrwaMoneyListSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            [weakSelf.dataArray addObjectsFromArray:[WYPutForward getPutForwardArrayFromJson:response[@"result"]]];
            [weakSelf.collectionView reloadData];
            weakSelf.collectionViewHeight.constant = weakSelf.collectionView.collectionViewLayout.collectionViewContentSize.height;
        }
        
        
    }];
}
- (IBAction)putForward:(id)sender {
    
//    if (!self.drawID.length) {
//        [self toast:@"请选择提现金额"];return;
//    }
    WEAKSELF
    if (self.moenyTextField.text.floatValue*1000>self.blance.floatValue){
        
         [self toast:@"您当前可用金额不足"];return;
        
    }
    if (self.moenyTextField.text.integerValue<10) {
        [self toast:@"最低提现金额为10元起"];return;
    }
    NSNumber *money =[NSNumber numberWithLong:self.moenyTextField.text.integerValue *1000];
    NSLog(@"%@",money);
    
    [WYHttpServer drawMoneyWithMoney:money SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            [weakSelf showSucess];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
    }];
    
    
    
    
    
//
//    [WYHttpServer drawMoneyWithId:self.drawID SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
//
//        if (success) {
////            [weakSelf toast:@"提现成功"];
////
////            int64_t delayInSeconds = 1.5; // 延迟的时间
////            __weak typeof(self)weakSelf = self;
////
////            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
////                [weakSelf.navigationController popViewControllerAnimated:YES];
////            });
//            [weakSelf showSucess];
//        }else{
//            [weakSelf toast:errorMessage];
//        }
//    }];
}

-(void)showSucess
{
    WEAKSELF
    [[WYPayAlertTooler sharedInstance] showAlertTitle:@"提现成功啦" message:@"" imageName:@"putWord_sucess_icon" btnTitles:@"我知道了" btnClicked:^(NSInteger index) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}




-(void)setupUI
{
    self.navigationItem.title = @"提现";
    self.mainView.width = kScreenWidth;
    self.mainView.height = kScreenHeight-self.navHeight;
    [self.scrollView addSubview:self.mainView];
    self.scrollView.contentSize = self.mainView.size;
    self.top.constant = self.navHeight;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    CGFloat itemW = (kScreenWidth-32-20)/3;
    
    layout.itemSize=CGSizeMake(itemW, itemW*0.8);
    //横向最小距离
    layout.minimumInteritemSpacing=1.0f;
    layout.minimumLineSpacing=1.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:layout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"WYPutForwardCell" bundle:nil] forCellWithReuseIdentifier:@"WYPutForwardCell"];
    self.balanceLabel.text = [NSString stringWithFormat:@"当前金额：%.2f",self.blance.floatValue/1000.0f];
    
     [self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#e38b78" alpha:1.0]];
     //[self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#999999" alpha:1.0]];
    self.putBtn.userInteractionEnabled  = NO;
    
    
//    if (self.isWithdraw.intValue==1){
//        [self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#999999" alpha:1.0]];
//        [self.putBtn setTitle:@"今日已提现" forState:(UIControlStateNormal)];
//    }
    
    if (self.oneWithdraw.integerValue==1) {
        self.messageLabel.text = @"首次提现最低1元起";
    }else{
        self.messageLabel.text = @"最低提现金额为10元起";
    }
    
    
    
    self.moenyTextField.borderStyle = UITextBorderStyleNone;
    
    [self.moenyTextField addTarget:self action:@selector(moenyTextFieldChanged:) forControlEvents:(UIControlEventEditingChanged)];
    
    
}
-(void)moenyTextFieldChanged:(UITextField *)textField
{
    if (textField.text.length) {
        [self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
        self.putBtn.userInteractionEnabled  = YES;
    }else{
        [self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#e38b78" alpha:1.0]];
        //[self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#999999" alpha:1.0]];
        self.putBtn.userInteractionEnabled  = NO;
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYPutForwardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYPutForwardCell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[NSBundle mainBundle]loadNibNamed:@"WYPutForwardCell" owner:nil options:nil].firstObject;
    }
    WYPutForward *forward = self.dataArray[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%.2f元",forward.money.floatValue/1000];
    cell.nameLabel.font = kFontSystemBIgger2(16);
    cell.backgroundColor = [UIColor hexStringToColor:@"#ffffff" alpha:1.0];
    [cell setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0f]];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     WYPutForward *forward = self.dataArray[indexPath.row];
    self.drawID = forward.id;
    
    
    WYPutForwardCell *cell =(WYPutForwardCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor hexStringToColor:@"#fffaf2" alpha:1.0];
    [cell setBorderWithWidth:kLineHeight andColor:kColorMainColor];
    NSLog(@"点击了一个cell");
    cell.nameLabel.textColor = kColorMainColor;
    
    if (self.isWithdraw.intValue!=1) {
        if (forward.money.floatValue>self.blance.floatValue) {
            [self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#e38b78" alpha:1.0]];
            //[self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#999999" alpha:1.0]];
            self.putBtn.userInteractionEnabled  = NO;
        }else{
            [self.putBtn setBackgroundColor:[UIColor hexStringToColor:@"#d8593f" alpha:1.0]];
            self.putBtn.userInteractionEnabled  = YES;
        }
    }else{
        
    }
    
    
    
    
    
    
    
   
    
    
    
    
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WYPutForwardCell *cell =(WYPutForwardCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor hexStringToColor:@"#ffffff" alpha:1.0];
    [cell setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0f]];
    cell.nameLabel.textColor = [UIColor blackColor];
    
    NSLog(@"取消选中了一个cll");
}








@end
